package com.example.demo.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.commom.Result;
import com.example.demo.entity.BookDao;
import com.example.demo.entity.BookReview;
import com.example.demo.mapper.BookReviewMapper;

@RestController
@RequestMapping("/bookReview")
public class BookReviewController
{
    @Resource
    BookReviewMapper bookReviewMapper;

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String search1,
            @RequestParam(defaultValue = "") String search2,
            @RequestParam(defaultValue = "") String search3,
            @RequestParam(defaultValue = "") String search4){

        LambdaQueryWrapper<BookReview> wrappers = Wrappers.<BookReview>lambdaQuery();
        if(StringUtils.isNotBlank(search1)){
            wrappers.like(BookReview::getBookNumber,search1);
        }
        if(StringUtils.isNotBlank(search2)){
            wrappers.like(BookReview::getBookName,search2);
        }
        if(StringUtils.isNotBlank(search3)){
            wrappers.like(BookReview::getUserId,search3);
        }
        if(StringUtils.isNotBlank(search4)){
            wrappers.like(BookReview::getUserName,search4);
        }
//        Page<LendRecord> LendRecordPage =LendRecordMapper.selectPage(new Page<>(pageNum,pageSize), wrappers);
        Page<BookReview> BookReviewPage =bookReviewMapper.selectPage(new Page<>(pageNum,pageSize), wrappers);
        return Result.success(BookReviewPage);
    }
    @PostMapping("/deleteReview")
    public  Result<?> deleteReview(@RequestBody BookReview bookReview){
        Map<String,Object> map = new HashMap<>();
        map.put("bookNumber",bookReview.getBookNumber());
        map.put("userId",bookReview.getId());
        bookReviewMapper.deleteByMap(map);
        return Result.success();
    }

    @PostMapping("/deleteReviews")
    public Result<?> deleteReviews(@RequestBody List<BookReview> bookReviews){
        int len = bookReviews.size();
        for(int i=0;i<len;i++) {
            BookReview bookreview = bookReviews.get(i);
            Map<String,Object> map = new HashMap<>();
            map.put("bookNumber",bookreview.getBookNumber());
            map.put("userId",bookreview.getUserId());
            bookReviewMapper.deleteByMap(map);
        }
        return Result.success();
    }

    @PostMapping("/reviewsadd")
    public Result<?> reviewsadd
            (@RequestBody BookReview bookReview)
            throws Exception
    {
//          System.out.println(bookReview);//已经有了bookname
//        return Result.success();
//        BookReview bookReview = new BookReview();
//        bookReview.setBookReview(BookReview);
//        bookReview.setBookNumber(BookNumber);
//        bookReview.setBookName(BookName);
//        bookReview.setUserId(UserId);
//        bookReview.setUserName(UserName);
//        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
//        Date date = new Date(System.currentTimeMillis());
//        String datestring = formatter.format(date);
//        date = formatter.parse(datestring);
//        bookReview.setCreateTime(date);
        bookReviewMapper.insert(bookReview);
        return Result.success();
    }
    @PostMapping("/test")
    public void test(@RequestBody BookReview book){
        System.out.println(book);
    }
}
